package com.jie.mc.event;


import com.jie.mc.Main;
import org.bukkit.Material;
import org.bukkit.block.Block;
import org.bukkit.block.BlockState;
import org.bukkit.block.Chest;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.EventPriority;
import org.bukkit.event.Listener;
import org.bukkit.event.block.Action;
import org.bukkit.event.player.PlayerInteractEvent;
import org.bukkit.event.player.PlayerJoinEvent;
import org.bukkit.inventory.Inventory;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.meta.ItemMeta;

public class PlayerEvent implements Listener {

    @EventHandler(priority = EventPriority.HIGH)
    public void PlayerJoin(PlayerJoinEvent event){
        Main.plugin.getLogger().info("触发了加入服务器事件:"+event.getPlayer().getName());
        event.getPlayer().sendMessage("§4欢迎至尊Vip进来:§c【"+event.getPlayer().getName()+"】");
        //event.getPlayer().teleport(new Location(Bukkit.getWorld("world"),0,70,0));
    }


//    @EventHandler
//    public void PlayerMove(PlayerMoveEvent event){
//        event.getPlayer().sendMessage("你不能移动");
//        event.setCancelled(true);
//    }


    @EventHandler
    public void PlayerUse(PlayerInteractEvent event){
        Player player=event.getPlayer();
        if (event.getAction()== Action.RIGHT_CLICK_BLOCK){

            //右键点击哪里都会触发
//            Player player = event.getPlayer();
//            ItemStack itemInHand = player.getItemOnCursor();
//            if(itemInHand!=null){
//                int amount = itemInHand.getAmount();
//                itemInHand.setAmount(amount+1);
//                player.setItemInHand(itemInHand);
//                player.sendMessage("数量成功增加一个");
//            }

//            BlockState state = event.getClickedBlock().getState();
//            state.setType(Material.AIR);
//            state.update();
//            Bukkit.getWorld("world").spawnFallingBlock(player.getLocation().add(3,0,3),Material.GOLD_BLOCK,(byte) 1);
            Block clickedBlock = event.getClickedBlock();
            if(clickedBlock.getType()== Material.CHEST){
                //判断是否是箱子

                BlockState state=clickedBlock.getState();
                Chest chest = (Chest) state;
                Inventory blockInventory=chest.getBlockInventory();
                ItemStack itemStack=new ItemStack(Material.EMERALD);
                itemStack.setAmount(64);
                ItemMeta itemMeta= itemStack.getItemMeta();
                itemMeta.setDisplayName("绿绿绿绿宝石");
                itemStack.setItemMeta(itemMeta);
                blockInventory.addItem(itemStack);

            }else {player.sendMessage("§d你这个不是箱子，无法添加");}
        }
    }
}

